Ext.define('jwawa.erp.framework.ResetTrigger', {
    extend : "Ext.form.field.Trigger",
    alias: 'widget.resetTrigger',
//    renderTpl: [
//        '<table id="{id}-bodyEl" width="100%;"><td>{[values.$comp.getSubTplMarkup()]}</td></div>'
//    ],
    mimicing: false,
    triggerBaseCls: 'resetTriggerSearchCell',
    triggerCls: 'resetTriggerSearchCellIcon',
    triggerWrapCls: 'resetTriggerWrap',
    triggerNoEditCls: Ext.baseCSSPrefix + 'trigger-noedit',
    focusCls: '',
    fieldCls: 'resetTriggerSearchInput',
    isSearchIcon: true,

    beforeInitComponent : function(){
        me = this;
        me.addEvents('inputChange');
    },

    initComponent: function() {
        var me = this;
        this.callParent(arguments);

        me.addListener('focus', function(obj, e, eOpts){
            me.inputEl.dom.placeholder = "";
        });

        me.addListener('blue', function(obj, e, eOpts){
            me.applyEmptyText();
        });

        me.addListener('keyup', function(obj, e, eOpts){
            var me = this,
                menuFilterBtn = me.triggerEl;
            if(obj.getValue() != null && obj.getValue() != ""){
                if (me.isSearchIcon == true){
                    menuFilterBtn.removeCls('resetTriggerSearchCellIcon');
                    menuFilterBtn.addCls('resetTriggerSearchCellResetIcon');
                    me.isSearchIcon = false;
                }
            } else {
                if (me.isSearchIcon == false){
                    menuFilterBtn.removeCls('resetTriggerSearchCellResetIcon');
                    menuFilterBtn.addCls('resetTriggerSearchCellIcon');
                    me.isSearchIcon = true;
                }
            }
            me.fireEvent('inputChange', me, e);
        });

        me.addListener('render', function(){
            var me = this,
                menuFilterBtn = me.triggerEl;
            menuFilterBtn.addListener('mouseover', function(e, t, eOpts){
                if(!me.isSearchIcon){
                    menuFilterBtn.addCls('resetTriggerSearchCellResetIconHover');
                }
            });

            menuFilterBtn.addListener('mouseout', function(e, t, eOpts){
                if(menuFilterBtn.hasCls('resetTriggerSearchCellResetIconHover')){
                    menuFilterBtn.removeCls('resetTriggerSearchCellResetIconHover');
                }
            });
        });
    },

    getSubTplMarkup: function() {
        var me = this,
            field = this.getTpl('fieldSubTpl').apply(this.getSubTplData());

        return '<table id="' + me.id + '-triggerWrap" class="' + me.triggerWrapCls + '" cellpadding="0" cellspacing="0"><tbody><tr>' +
             me.getTriggerMarkup() +
            '<td id="' + me.id + '-inputCell" class="' + Ext.baseCSSPrefix + 'form-trigger-input-cell">' + field + '</td>' +
            '</tr></tbody></table>';
    },

    onTriggerClick: function(e){
        var me = this,
            menuFilterBtn = me.triggerEl;

        if(menuFilterBtn.hasCls('resetTriggerSearchCellResetIconHover')){
            menuFilterBtn.removeCls('resetTriggerSearchCellResetIconHover');
        }
        menuFilterBtn.removeCls('resetTriggerSearchCellResetIcon');
        menuFilterBtn.addCls('resetTriggerSearchCellIcon');
        me.isSearchIcon = true;
        me.reset();

        me.fireEvent('inputChange', me, e);
    }
});